﻿package Modules.MouseCursorModule
{
	import Modules.MainTimeLine;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class RedecorateTool extends Tool
	{
		//Current Object is the current object that the mouse is pointing at
		var currentObject:MovieClip;
		
		//Current Selected Object is the object that the player selects with a click
		public var currentSelectedObject:MovieClip;
		public var selectEvent:MouseEvent;
		
		//Constructor
		public function RedecorateTool()
		{
			super();
		}
		
		//Function called when the mouse moves when in redecoration mode
		override public function toolMove(event:MouseEvent):void{
			
			//Check against the target and see if it is a crop or building
			if((event.target is CropDisplayObject)||(event.target is BuildingDisplayObject)){
				
				if(currentObject != event.target){
					
					//Unhighlight previous object
					if(currentObject != null){
						currentObject.unhighlightObject();
					}
					
					//Highlight current object (the one the mouse is over. Not selected yet)
					currentObject = event.target as MovieClip;
					selectEvent = event;
					currentObject.highlightObject();
					
					if(event.target is BuildingDisplayObject){
						MainTimeLine.decoHUD.targetObject = currentObject;
					}
					
					if(event.target is CropDisplayObject){
						MainTimeLine.cropDecoHUD.targetObject = currentObject;
					}
				}
			}else{
				
				//unhighlight object because no valid object is detected
				if(currentObject != null){
					currentObject.unhighlightObject();
					currentObject = null;
				}
			}
		}
		
		//Called when the mouse is clicked
		override public function toolUp(event:MouseEvent):void{
			
			//Call super to check if it is a click or a hold
			super.toolUp(event);
			
			//Clean up to hide the redecoration HUD
			cleanUp();
			
			//if is a click, set HUD to the objec thte mouse is currently pointing to
			if(click){
				if(currentObject != null){
					
					//HUD offset different from object to object
					//objective is to center it.
					var widthOffset:Number;
					var heightOffset:Number;
					
					//Select the current Object
					currentSelectedObject = currentObject;
					
					//check what kind of object the selected object is
					//Should have two kinds
					//crop or building
					//Following lines of codes just deals with setting the position of the HUD
					if(currentObject is CropDisplayObject){
						MainTimeLine.cropDecoHUD.visible = true;
						widthOffset = (currentObject.width - 98.15)*0.5;
						heightOffset = (currentObject.height - 55)*0.5;
						
						MainTimeLine.cropDecoHUD.x = currentObject.x + widthOffset;
						MainTimeLine.cropDecoHUD.y = currentObject.y + heightOffset;
					}else {
						
						MainTimeLine.decoHUD.visible = true;
						
						widthOffset = (currentObject.width - 98.15)*0.5;
						heightOffset = (currentObject.height - 55)*0.5;
						
						MainTimeLine.decoHUD.x = currentObject.x + widthOffset;
						MainTimeLine.decoHUD.y = currentObject.y + heightOffset;
					}
				}
			}
		}
		
		//Hide the HUDs
		public function cleanUp():void{
			MainTimeLine.cropDecoHUD.visible = false;
			MainTimeLine.decoHUD.visible = false;
		}
	}
}